const seq = require("../db/seq");
const { QueryTypes, Op, where } = require("sequelize");
const dictInfo = require("../model/dictInfo.model");
const Nav = require("../model/nav.model");
const FriendLink = require("../model/friendLink.model");
const Limit = require("../model/limit.model");
class commonService {
  async queryEnumTypeService(dictId) {
    const res = await seq.query("select * from dictype where dictId=?", {
      replacements: [dictId],
      type: QueryTypes.SELECT,
    });
    console.log("----common", res);
    return res;
  }
  async queryDictService(requestObj) {
    if (requestObj && requestObj.dictName) {
      requestObj.dictName = {
        [Op.like]: `%${requestObj.dictName}%`,
      };
    }
    const res = await dictInfo.findAll({ where: requestObj });
    return res;
  }

  async queryDictByIdService(dictId) {
    const res = await seq.query(
      "select * from `dictype` where dictId=:dictId",
      {
        type: QueryTypes.SELECT,
        replacements: { dictId },
      }
    );
    for (let i = 0; i < res.length; i++) {
      const dictTypes = await seq.query(
        "select * from `bl-dictinfo` where id=:id",
        {
          type: QueryTypes.SELECT,
          replacements: { id: dictId },
        }
      );
      const { dictType, dictName } = dictTypes[0];
      res[i].dictType = dictType;
      res[i].dictName = dictName;
    }
    console.log(res, "??");
    return res;
  }

  // 查询导航类型
  async queryNavTypeService(requestObj) {
    if (requestObj.name) {
      const res = await seq.query(
        "select * from `bl-nav` where name like :name",
        {
          type: QueryTypes.SELECT,
          replacements: { name: `%${requestObj.name}%` },
        }
      );
      return res;
    } else {
      const res = await seq.query("select * from `bl-nav`", {
        type: QueryTypes.SELECT,
      });
      return res;
    }
  }

  // 查询网址导航列表
  async queryNavListService(requestObj) {
    let paramsObj = {};
    if (requestObj.typeId) {
      paramsObj.typeId = requestObj.typeId;
    }
    const res = await Nav.findAll({ where: paramsObj });
    return res;
  }
  // 修改导航列表信息
  async modifyNavByIdService(requestObj) {
    const { id, ...searchObj } = requestObj;
    const res = await Nav.update(searchObj, { where: { id } });
    return res;
  }

  // 友情链接
  async queryFriendLinkService() {
    const res = await FriendLink.findAll();
    return res;
  }

  // 修改友情链接的信息
  async modifyFriendLinkByIdService(requestObj) {
    const { id, ...searchObj } = requestObj;
    const res = await FriendLink.update(searchObj, { where: { id } });
    return res;
  }

  // 添加友情链接的信息
  async addFriendLinkService(requestObj) {
    const res = await FriendLink.create(requestObj);
    return res;
  }

  //
  async addNavControlService(requestObj) {
    const res = await Nav.create(requestObj);
    return res;
  }
  // 权限部分
  async queryLimitService() {
    const res = await Limit.findAll();
    return res;
  }

  async addLimitService(requestObj) {
    const res = await Limit.create(requestObj);
    return res;
  }

  async modifyLimitService(requestObj) {
    const { id, ...searchObj } = requestObj;
    const res = await Limit.update(searchObj, { where: { id } });
    return res;
  }

  async deleteLimitService(id) {
    const res = await Limit.destroy({
      where: {
        id,
      },
    });
    return res;
  }
}

module.exports = new commonService();
